package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.anhua.HelloService;
import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    public UserService userService;
    @Autowired
    private HelloService helloService;
    @RequestMapping("/hello")
    public String sayHello(){
        return helloService.sayHello();
    }
//    @RequestMapping("/getAllUser")
//    public List<User> getAllUser(HttpServletResponse response, HttpServletRequest request){
//        response.setHeader("Access-Control-Allow-Origin","*");
//        response.setCharacterEncoding("utf-8");
//        List<User> users = userService.selAllUser();
//        return users;
//    }
//    @RequestMapping("/getOne/{id}")
//    public User getUser(HttpServletResponse response,  @PathVariable("id") int id){
//        response.setHeader("Access-Control-Allow-Origin","*");
//        response.setCharacterEncoding("utf-8");
//        User user = userService.selUserById(id);
//        return user;
//    }
//    @RequestMapping("/getAllUser2")
//    public List<User> getAllUser2(){
//        return (List<User>) restTemplate.getForObject("http://localhost:8081/getAllUser",User.class);
//    }
//    @RequestMapping("/getOne82/{id}")
//    public User getUser(HttpServletResponse response, @PathVariable("id") int id){
//        User forObject = restTemplate.getForObject("http://localhost:8081/getOne/"+id, User.class);
//        return forObject;
//    }
@RequestMapping("/getAllUser2")
public String getUser2(HttpServletResponse response){
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setCharacterEncoding("utf-8");
    return "杨安华";
}

    @RequestMapping("/getUserByid")
    public Map getUserById(@RequestBody String data){
//        System.out.println("获取到的参数"+id);
//
//        User id1 = JSONObject.toJavaObject(id, User.class);
//        System.out.println("装换号参数"+id1);
//        Map<String,String> map=new HashMap<String ,String>();
//        map.put("id",id1.getId());
//        map.put("username",id1.getUsername());
//        User user = userService.selUserById(map);
        Map jsonObject = JSON.parseObject(data);

        User user = userService.selUserById(jsonObject);
        Map map=new HashMap();
        String code="1";
        String msg="";
        if (user==null){
            code="0";
            msg="没有查询到信息";
        }else {
            msg=user.toString();
        }
        map.put("code",code);
        map.put("msg",msg);
        return map;
    }
    @RequestMapping("/hello2")
    public String sayHello2(){
        System.out.println("亚感化");
        return "杨安华";
    }

}
